//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <fcntl.h>

#include <time.h>
#include <errno.h>

#include <sys/stat.h>
#include <sys/times.h>
#include <sys/wait.h>

#include <iostream>
#include <fstream>

#include "libraries.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define  LENGTHTEXT                 10000
#define  NUMBERRESIDUESEXTEND       6
#define  PROGRAM_GETEXTENDALLBS     "bin/GetExtendedAllForBindingRegion"
#define  PROGRAM_PRAT2              "extern/Pratt2.1/pratt"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
typedef char t_string [LENGTHTEXT];
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void panic (const char*);
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main (int argc, char** argv) {
  
  String* filenameDirectories       = NULL;
  String* pathToProteinLINGOProject = NULL;
  String* pathToCATHFastaProfile    = NULL;
  
  if (argc < 4) {
    
    cout << "Error in the input parameters! Must by like: " << endl;
    cout << "./PipeProcessGetPatternsFromCATH <In File - List of Directories to process>";
    cout <<                                 " <Path to Protein LINGO Project executable>";
    cout <<                                 " <Path to CATH Fasta Profile File>";
    cout << endl;
    
    return 0;
    
  }
  else {
    
    filenameDirectories       = new String(argv[1]);
    pathToProteinLINGOProject = new String(argv[2]);
    pathToCATHFastaProfile    = new String(argv[3]);
    
    cout << "------------------------------------------------------------------------------------------------------" << endl;
    cout << " In File - List of Directories to process : " << argv[1] << endl;
    cout << " Path to Protein LINGO Project executable : " << argv[2] << endl;
    cout << "          Path to CATH Fasta Profile File : " << argv[3] << endl;
    cout << "------------------------------------------------------------------------------------------------------" << endl;
    
  }
  
  pathToProteinLINGOProject->Trim();
  
  //Directory with the name of folder to process (the folder with the information of residues near to the ligands ordered by distance in angstrom)
  ifstream directoriesFile (filenameDirectories->Out());
  if (!directoriesFile.is_open()) return EXIT_FAILURE;
  cout << "Opening the file with the directories to process - " << filenameDirectories->Out() << endl;
  
  char    buffer [LENGTHTEXT + 1];
  String* bufferString = new String ();
  
  //Loop across the folders
  while (!directoriesFile.eof()) {
    
    directoriesFile.getline(buffer, LENGTHTEXT);
    
    bufferString->In(buffer);
    bufferString->Trim();
    
    if (chdir(bufferString->Out()) == -1) panic ("Error changing directory");

    cout    << "Changing to a directory of data " << bufferString->Out() << endl;
    
    if ((pathToProteinLINGOProject->Out()[pathToProteinLINGOProject->Length() - 1] != '/')) 
      pathToProteinLINGOProject->Add("/");
        
    //Get the information in FASTA format of the extended profile near to the residues in the binding site using the program GetExtendedAllForBindingRegion
    system("cp anchor.residues.dat input\0");
    cout << "Executing the Program GetExtendedAllForBindingRegion" << endl;
    sprintf(buffer, "%s%s \0",  pathToProteinLINGOProject->Out(), PROGRAM_GETEXTENDALLBS);
    sprintf(buffer, "%s input %s %d epitopes\0", buffer, pathToCATHFastaProfile->Out(), NUMBERRESIDUESEXTEND);
    system(buffer); 
        
    //Get the patterns with the Pratt 2.1 program
    system("cp epitopes.Extended.AA.dat input\0");

    //90%
    sprintf(buffer, "%s%s \0",  pathToProteinLINGOProject->Out(), PROGRAM_PRAT2);
    sprintf(buffer, "%s fasta input -C%% 90 -PL 50 -PN 50 -PX 5 -FN 3 -FL 5 -FP 104 -RG ON -OF output -ON 5000 -OA 0 \0", buffer);
    system(buffer);      

    sprintf(buffer, "cp output patterns.AA.Pratt2.1.90P.dat\0");
    system(buffer);      

    //80%
    sprintf(buffer, "%s%s \0",  pathToProteinLINGOProject->Out(), PROGRAM_PRAT2);
    sprintf(buffer, "%s fasta input -C%% 80 -PL 50 -PN 50 -PX 5 -FN 3 -FL 5 -FP 104 -RG ON -OF output -ON 5000 -OA 0 \0", buffer);
    system(buffer);      

    sprintf(buffer, "cp output patterns.AA.Pratt2.1.80P.dat\0");
    system(buffer);      

    //60%
    sprintf(buffer, "%s%s \0",  pathToProteinLINGOProject->Out(), PROGRAM_PRAT2);
    sprintf(buffer, "%s fasta input -C%% 60 -PL 50 -PN 50 -PX 5 -FN 3 -FL 5 -FP 104 -RG ON -OF output -ON 5000 -OA 0 \0", buffer);
    system(buffer);      

    sprintf(buffer, "cp output patterns.AA.Pratt2.1.60P.dat\0");
    system(buffer);      

    //40%
    sprintf(buffer, "%s%s \0",  pathToProteinLINGOProject->Out(), PROGRAM_PRAT2);
    sprintf(buffer, "%s fasta input -C%% 40 -PL 50 -PN 50 -PX 5 -FN 3 -FL 5 -FP 104 -RG ON -OF output -ON 5000 -OA 0 \0", buffer);
    system(buffer);      

    sprintf(buffer, "cp output patterns.AA.Pratt2.1.40P.dat\0");
    system(buffer);      

    //20%
    sprintf(buffer, "%s%s \0",  pathToProteinLINGOProject->Out(), PROGRAM_PRAT2);
    sprintf(buffer, "%s fasta input -C%% 20 -PL 50 -PN 50 -PX 5 -FN 3 -FL 5 -FP 104 -RG ON -OF output -ON 5000 -OA 0 \0", buffer);
    system(buffer);      

    sprintf(buffer, "cp output patterns.AA.Pratt2.1.20P.dat\0");
    system(buffer);      

    system("rm input output report\0");      
      
    
  }
  
  directoriesFile.close();
  
  if (bufferString)              delete bufferString;
  if (filenameDirectories)       delete filenameDirectories;
  if (pathToProteinLINGOProject) delete pathToProteinLINGOProject;
  if (pathToCATHFastaProfile)    delete pathToCATHFastaProfile;

  return EXIT_SUCCESS;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void panic (const char *str) {
  
  t_string s;

  cout << str << endl;
  
  write(2, str, strlen(str));
  write(2, "\n", 1);
  
  if (errno != 0) {
    
    sprintf(s, "%d %s\n", errno, strerror(errno));
    write(2, s, strlen(s));
    
  }
  
  exit(0);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
